package com.jiantong.work.util;

import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.model.User;
import com.jiantong.user.model.UserProfile;

import java.util.Calendar;

/**
 * Created by niufei on 2017/4/5.
 *
 */
public class Utils {

    private final static int LENGTH = 5;

    /**
     * 生成单据前缀
     * @param prefix 单据编号前缀的开头字母
     * @param loginUser 当前用户
     * @return prefix
     */
    public static String createPrefix(String prefix , LoginUser loginUser){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;

        UserProfile userProfile = loginUser.getUserProfile();
        Integer provinceId = userProfile.getProvinceId();
        Integer cityId = userProfile.getCityId();
        Integer regionId = userProfile.getRegionId();
        if(provinceId < 10){
            prefix = prefix + 0 + provinceId;
        }else{
            prefix = prefix + provinceId;
        }

        if(cityId < 10){
            prefix = prefix + 0 + cityId;
        }else{
            prefix = prefix + cityId;
        }

        if(regionId < 10 ){
            prefix = prefix + 0 + regionId;
        }else{
            prefix = prefix + regionId;
        }

        if(month < 10){
            prefix = prefix + year % 100 + 0 + month;
        }else{
            prefix = prefix + year % 100 + month;
        }
        return prefix;
    }

    /**
     * 生成单据编号
     * @param prefix 单据前缀
     * @param maxNumber 数据库里的当前对应单据好
     * @return number
     */
    public static String create(String prefix , String maxNumber){
        String number = prefix;
        if(maxNumber == null){
            for(int i = 1 ; i <= LENGTH;i++){
                if(i != LENGTH){
                    number += 0;
                }else{
                    number += 1;
                }
            }
        }else {
            String tempNumber = maxNumber.substring(maxNumber.length()-5,maxNumber.length());
            int i = 0;
            for(;i < tempNumber.length();i++){
                if(tempNumber.indexOf(i) != 0){
                    break;
                }
            }
            tempNumber = tempNumber.substring(i,tempNumber.length());
            int aa = Integer.parseInt(tempNumber);
            aa = aa + 1;
            tempNumber = "" + aa;
            int length  = LENGTH - tempNumber.length();

            for(i = 0;i<length;i++){
                number += 0;
            }
            number += tempNumber;
        }

        return number;
    }
}

